/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jforum.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import jforum.domain.User;
import jforum.util.DBConnector;
import jforum.security.MD5Util;

/**
 *
 * @author Вадим
 */
public class UserDAO implements BasicDAO<User> {
    private DBConnector connector;

    public UserDAO(DBConnector conn) {
        this.connector=conn;
    }

    private User getFromResultSet(ResultSet rs) throws SQLException {
        return new User(rs.getInt("user_id"),rs.getString("login"),rs.getString("pass"), rs.getLong("reg_time"),
                    rs.getString("email"),rs.getString("act_key"),rs.getShort("active"),rs.getString("secure"),
                    rs.getInt("msg_count"),rs.getString("avatar"), rs.getInt("rights"));
    }

    public User Load(int id) throws SQLException {
        User user=null;
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"user\" WHERE user_id=?;", id);
        if (rs.next())
        {
            user=this.getFromResultSet(rs);
        }
        rs.close();
        return user;
    }

    public User Load(String login) throws SQLException {
        User user=null;
        ResultSet rs=this.connector.executePreparedQuery("SELECT * FROM \"user\" WHERE login=?;",login);
        if (rs.next())
        {
            user=this.getFromResultSet(rs);
        }
        rs.close();
        return user;
    }

    public User LoadOrCreate(int id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<User> LoadList() throws SQLException {
        return LoadByQuery("SELECT * FROM \"user\";");
    }

    public List<User> LoadCandidates() throws SQLException {
        return LoadByQuery("SELECT * FROM \"user\" WHERE active=0;");
    }
    
    public List<User> LoadActive() throws SQLException {
        return LoadByQuery("SELECT * FROM \"user\" WHERE active=1;");
    }

    private List<User> LoadByQuery(String query) throws SQLException {
        List<User> list = new LinkedList<User>();
        ResultSet rs=this.connector.executeQuery(query);
        while (rs.next())
        {
            User user=this.getFromResultSet(rs);
            list.add(user);
        }
        rs.close();
        return list;
    }

    public List<User> LoadPage(int startRowIndex, int maximumRows) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Activate(int id) throws SQLException {
        this.connector.executePreparedUpdate("UPDATE \"user\""+
                " SET active=1 WHERE user_id=?;", id);
    }

    public void Punish(int id) throws SQLException {
        this.connector.executePreparedUpdate("UPDATE \"user\""+
                " SET active=-1 WHERE user_id=?;", id);
    }

    public void Save(User obj) throws SQLException {     
        this.connector.executePreparedUpdate("INSERT INTO \"user\""+
                " (login, pass, reg_time, email, act_key , active, rights)"+
                " VALUES (?, ?, ?, ?, ?, ?, ?);", obj);
    }

    public void Update(User obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void Delete(User obj) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void DeleteAll() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int GetCount() throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
